using Admin2025.Application.common;
using Admin2025.Application.Dtos;
using Admin2025.Domain.Entities.App;
using Admin2025.Domain.Repository;

namespace Admin2025.Application.Services;

public class AppDepartmentService : IAppDepartmentService
{
    private readonly IRepository<AppDepartment> _deptRepo;
    public AppDepartmentService(IRepository<AppDepartment> deptRepo)
    {
        _deptRepo = deptRepo;
    }

    public async Task<dynamic> CreateDepartmentAsync(CreateDepartmentDto dto)
    {
        var isExists = _deptRepo.Table.Any(x => x.DepartmentName == dto.DepartmentName && x.ParentId == dto.ParentId);
        if (isExists)
        {
            return ApiResult.Fail(3002, "同级下已存在同部门，请确认重试");
        }
        var dept = new AppDepartment { DepartmentName = dto.DepartmentName, ParentId = dto.ParentId };
        await _deptRepo.CreateAsync(dept);
        return ApiResult.Success(dept, "创建部门成功");
    }
    public async Task<dynamic> DeleteDepartmentAsync(Guid depId)
    {
        var dept = await _deptRepo.GetByIdAsync(depId);
        if (dept == null)
        {
            return ApiResult.Fail(4004, "要删除的部门不存在，请确认后重试");
        }
        if (dept.IsDeleted)
        {
            return ApiResult.Fail(4004, "部门已被删除，请确认后重试");
        }
        dept.IsDeleted = true;
        await _deptRepo.UpdateAsync(dept);
        return 
    }
    public Task<dynamic> UpdateDepartmentAsync(Guid depId, UpdateDepartmentDto dto)
    {

    }
    public Task<dynamic> EnableDepartment(Guid deptId)
    {

    }
    public Task<dynamic> DisableDepartment(Guid deptId)
    {

    }

}